﻿using DtuService;
using NetCommand;
using SuperSocket.SocketBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DtuService
{
    public enum NET_SERVER_TYPE
    {
        TCP,
        HTTP
    }

    public class Client : IClient
    {
        public ByteSession bSession;

        public int ID
        {
            set;
            get;
        }

        public string IP
        {
            get
            {
                return "";
            }
        }

        public Client(ByteSession session)
        {
            ID = 0;
            this.bSession = session;
        }

        public virtual void ProcessMessage(NetCommand.NetCommand cmd)
        {
            Debug.LogDebug("解析消息");
            // TODO  解析
            despitchCmd(cmd);
        }

        private void despitchCmd(NetCommand.NetCommand msg)
        {
            if (msg.Header.CMD_ID < (ushort)CMD_ID_DEFINE.USER_CMD_ID_BEGIN)
            {
                receiveDeviceMessage(msg);
            }
        }
        
        protected virtual void receiveUserMessage(NetCommand.NetCommand msg)
        {
            
        }

        protected virtual void receiveDeviceMessage(NetCommand.NetCommand cmd)
        {
 
        }

        protected virtual void userLoginSuccess()
        {

        }

        public virtual void Update(float deltaTime)
        {
 
        }

        public void Shutdown()
        {
            bSession.Close();
            OnShutdown();
        }
        public virtual void OnShutdown()
        {
           
        }

        public virtual void SendMessage(NetCommand.NetCommand msg)
        {
            if (bSession == null)
            {
                Debug.Log("网络连接不存在");
                return;
            }
            if (bSession.SocketSession == null)
            {
                Debug.Log("网络连接套接字不存在");
                return;
            }
        }

        public virtual void SendMessage(byte[] msg)
        {
            if (bSession == null)
            {
                Debug.Log("网络连接不存在");
                return;
            }
            if (bSession.SocketSession == null)
            {
                Debug.Log("网络连接套接字不存在");
                return;
            }
            bSession.SendMessage(msg);
        }

        public void Start()
        {
            
        }

        public void Exception(Exception e)
        {
            Debug.Error(e.ToString());
        }

        public void Close(CloseReason reason)
        {
            Debug.LogDebug(reason.ToString());
        }
    }
}
